package com.yanfan.zutai.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.taosdata.jdbc.TSDBDriver;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Properties;

/**
 * TODO: 下个版本将与 com.yanfan.iot.tdengine.config.TDengineConfig 进行融合
 */
@Component
public class TaosConnectTool {
    private static volatile TaosConnectTool taosConnectTool = null;
    @Value("${spring.datasource.druid.tdengine-server.enabled}")
    private Boolean enabled;
    @Value("${spring.datasource.druid.tdengine-server.url}")
    private String dbUrl;
    @Value("${spring.datasource.druid.tdengine-server.username}")
    private String username;
    @Value("${spring.datasource.druid.tdengine-server.password}")
    private String password;
    @Value("${spring.datasource.druid.tdengine-server.driverClassName}")
    private String driverClassName;
    @Value("${spring.datasource.druid.tdengine-server.precision:us}")
    private String precision;
    @Value("${spring.datasource.druid.tdengine-server.keep:3650}")
    private Integer keep;
    @Value("${spring.datasource.druid.tdengine-server.duration:1}")
    private Integer duration;
    @Value("${spring.datasource.druid.tdengine-server.buffer:32}")
    private Integer buffer;

    private DruidPooledConnection pooledConnection;
    /**
     * -- GETTER --
     * 获取连接池的jdbc
     */
    @Getter
    @Setter
    private JdbcTemplate jdbcTemplate;

    public static TaosConnectTool getInstance() {
        if (taosConnectTool == null) {
            synchronized (TaosConnectTool.class) {
                if (taosConnectTool == null) {
                    taosConnectTool = new TaosConnectTool();
                }
            }
        }
        return taosConnectTool;
    }

    /**
     * 创建连接,@PostConstruct注入到容器中
     */
    @PostConstruct
    public void connect() {
        if (!enabled) {
            System.out.println("不启动taos连接池");
            return;
        }
        taosConnectTool = this;
        DruidDataSource dds = new DruidDataSource();
        try {
            System.out.println("---------------taos服务开始启动---------------");
            dds.setUrl(dbUrl);
            Properties connProps = new Properties();
            connProps.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8");
            connProps.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8");
            connProps.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8");
            dds.setConnectProperties(connProps);
            dds.setUsername(username);
            dds.setPassword(password);
            dds.setDriverClassName(driverClassName);// com.taosdata.jdbc.rs.RestfulDriver、com.taosdata.jdbc.TSDBDriver
            dds.setInitialSize(5);
            dds.setMinIdle(10);
            dds.setMaxActive(20);
            dds.setMaxWait(60000);
            dds.setTimeBetweenEvictionRunsMillis(60000);
            dds.setMinEvictableIdleTimeMillis(300000);
            dds.setMaxEvictableIdleTimeMillis(900000);
            dds.setValidationQuery("select server_status();");
            pooledConnection = dds.getConnection();
            jdbcTemplate = new JdbcTemplate(dds);
            System.out.println("---------------taos服务启动成功---------------");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
